package com.xbs.dao.enterprise;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.EnterpriseOrderEntity;
import com.xbs.entity.EnterpriseOrderInfoEntity;
import com.xbs.entity.EnterpriseOrderQueryEntity;
import com.xbs.utils.EnterpriseUtils;

public class EnterpriseSelfOrderDao extends BaseDao{
	/**
	 * 自选订单查询
	 */
	public List<EnterpriseOrderEntity> getSelfOrder(String account){
		EnterpriseUtils util=new EnterpriseUtils();
		
		String sql="select eo_order_id,eo_establish_time,eo_reserve_get_time,eo_reserve_get_place,eo_order_status,eo_plan_money"
				+ " from tb_enterprise_order where "
				+ "eo_rent_type=0 and eo_account=? and DATE_FORMAT(eo_establish_time,'%Y-%m-%d')=?";
		Date d=Date.valueOf(util.getSimpleDate());
		System.out.println(sql);
		RowDatas<EnterpriseOrderEntity> rd=new RowDatas<EnterpriseOrderEntity>() {
			@Override
			public EnterpriseOrderEntity getRowData(ResultSet rs) throws SQLException {
				EnterpriseOrderEntity epoe=new EnterpriseOrderEntity();
				epoe.setEo_order_id(rs.getString("eo_order_id"));
				epoe.setEo_order_status(rs.getString("eo_order_status"));
				epoe.setEo_plan_money(rs.getDouble("eo_plan_money"));
				epoe.setEo_reserve_get_place(rs.getString("eo_reserve_get_place"));
				epoe.setEo_reserve_get_time(rs.getDate("eo_reserve_get_time"));
				epoe.setEo_establish_time(rs.getTimestamp("eo_establish_time"));
				return epoe;
			}
		};
		return queryList(sql, new ParamSet(account,d), rd);
	}
	/**
	 * 根据多条件查询订单
	 */
	public List<EnterpriseOrderEntity> getSelfOrderMulcondition(EnterpriseOrderQueryEntity e){
		String sql="select eo_order_id,eo_reserve_get_time,eo_reserve_get_place,eo_order_status,eo_plan_money"
				+ " from tb_enterprise_order where "
				+ "eo_rent_type=? and eo_account=? and DATE_FORMAT(eo_establish_time,'%Y-%m-%d')>=? and DATE_FORMAT(eo_establish_time,'%Y-%m-%d')<=?";
		if(!e.getState().equals("全部")){
			sql+=" and eo_order_status="+e.getState();
		}
		RowDatas<EnterpriseOrderEntity> rd=new RowDatas<EnterpriseOrderEntity>() {
			
			@Override
			public EnterpriseOrderEntity getRowData(ResultSet rs) throws SQLException {
				EnterpriseOrderEntity epoe=new EnterpriseOrderEntity();
				epoe.setEo_order_id(rs.getString("eo_order_id"));
				epoe.setEo_order_status(rs.getString("eo_order_status"));
				epoe.setEo_plan_money(rs.getDouble("eo_plan_money"));
				epoe.setEo_reserve_get_place(rs.getString("eo_reserve_get_place"));
				epoe.setEo_reserve_get_time(rs.getDate("eo_reserve_get_time"));
				return epoe;
			}
		};
		System.out.println("sdfsdfsdfdsf");
		System.out.println(sql);
		return queryList(sql, new ParamSet(e.getRentState(),e.getAccount(),e.getStartTime(),e.getEndTime()),rd );
	}
	
	/**
	 * 分页查询企业自选订单
	 */
	public List<EnterpriseOrderEntity> getSelfOrderMulconditionPage(EnterpriseOrderQueryEntity e,Integer currPage,Integer pageSize){
		Integer start=(currPage-1)*pageSize;
		String sql="select eo_order_id,eo_reserve_get_time,eo_reserve_get_place,eo_order_status,eo_plan_money"
				+ " from tb_enterprise_order where "
				+ "eo_rent_type=? and eo_account=? and DATE_FORMAT(eo_establish_time,'%Y-%m-%d')>=? and DATE_FORMAT(eo_establish_time,'%Y-%m-%d')<=? ";
		if(!e.getState().equals("全部")){
			//sql+=" and eo_order_status="+e.getState();
			sql+="and eo_order_status='"+e.getState()+"'";
		}
		sql+=" limit ?,?";
		RowDatas<EnterpriseOrderEntity> rd=new RowDatas<EnterpriseOrderEntity>() {
			
			@Override
			public EnterpriseOrderEntity getRowData(ResultSet rs) throws SQLException {
				EnterpriseOrderEntity epoe=new EnterpriseOrderEntity();
				epoe.setEo_order_id(rs.getString("eo_order_id"));
				epoe.setEo_order_status(rs.getString("eo_order_status"));
				epoe.setEo_plan_money(rs.getDouble("eo_plan_money"));
				epoe.setEo_reserve_get_place(rs.getString("eo_reserve_get_place"));
				epoe.setEo_reserve_get_time(rs.getDate("eo_reserve_get_time"));
				
				return epoe;
			}
		};
		
		return queryList(sql, new ParamSet(e.getRentState(),e.getAccount(),e.getStartTime(),e.getEndTime(),currPage,pageSize),rd );
	}
	/**
	 * 返回自选企业订单的总记录数
	 */
	public Integer getEnterpriseSelfCount(EnterpriseOrderQueryEntity e){
		String sql="select count(*) as sum"
				+ " from tb_enterprise_order where "
				+ "eo_rent_type=? and eo_account=? and eo_reserve_get_time>? and eo_reserve_get_time<?";
		if(!e.getState().equals("全部")){
			//sql+=" and eo_order_status="+e.getState();
			sql+="and eo_order_status='"+e.getState()+"'";
		}
		RowDatas<Integer> rd=new RowDatas<Integer>(){

			@Override
			public Integer getRowData(ResultSet rs) throws SQLException {
				return rs.getInt("sum");
			}
			
		};
		return query(sql, new ParamSet(e.getRentState(),e.getAccount(),e.getStartTime(),e.getEndTime()),rd);
		
	}
	
	/**
	 * 根据企业订单编号查询详细信息
	 */
	public List<EnterpriseOrderInfoEntity> getOrderInfoById(String id){
		String sql="select eo_account,eo.eo_order_id,eo_reserve_get_time,eo_fact_return_time,eo_order_status,eo_rent_type,cm.car_model_id,car_model_number,rent_money,car_model_name,car_model_type,car_model_seating,car_model_fuel_type,car_model_image,company_discount from tb_car_model cm,tb_enterprise_model em,tb_enterprise_order eo where eo.eo_order_id =em.eo_order_id and em.car_model_id=em.car_model_id";
		return null;
	}
}
